package com.neu.question.controller;


import com.neu.question.beans.PageVO;
import com.neu.question.beans.ResultVO;
import com.neu.question.domain.EndUserInfo;
import com.neu.question.domain.RespondentInfo;
import com.neu.question.domain.UserInfo;
import com.neu.question.form.EndUserForm;
import com.neu.question.form.SearchForm;
import com.neu.question.service.RespondentInfoService;
import com.neu.question.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@RequestMapping("/respond")
public class RespondentController {

    @Autowired
    private RespondentInfoService respondentInfoService;

    @Autowired
    private UserInfoService userInfoService;




    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageVO list = respondentInfoService.list(page, size);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setData(list);
        return resultVO;
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm) {
//        System.out.println(searchForm.getKey());
//        System.out.println(searchForm.getValue());
        PageVO list = respondentInfoService.search(searchForm);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setData(list);
        return resultVO;
    }


    @PostMapping("/save")
    public ResultVO save(@RequestBody EndUserForm endUserForm) {
//        System.out.println("**********");
//        System.out.println(endUserForm.getStartTime());
//        System.out.println(endUserForm.getStopTime());
//        System.out.println(endUserForm.getUsername());

        int result = respondentInfoService.save(endUserForm);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(result);
        resultVO.setData("3用户名存在，4电话号存在，5未存入RespondentInfo ,1成功");
        return resultVO;
//        return null;
    }


    @GetMapping("/findById/{id}")
    public ResultVO findById(@PathVariable("id") String id) {
        ResultVO resultVO = new ResultVO();
        RespondentInfo endUserInfo = respondentInfoService.getById(id);
        UserInfo userInfo = userInfoService.getById(endUserInfo.getUserId());
        EndUserForm endUserForm = new EndUserForm();
        endUserForm = endUserForm.create(userInfo, endUserInfo.getEmail(), endUserInfo.getQuestionnaireId());
        resultVO.setCode(0);
        resultVO.setData(endUserForm);
        return resultVO;
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody EndUserForm endUserForm) {
        //更新数据
        // 判断时间先后
        ResultVO resultVO = new ResultVO();
        int update;
        boolean use = false;
//        System.out.println(12);
        System.out.println(endUserForm.getStopTime());
        if (endUserForm.getStopTime().after(endUserForm.getStartTime())) {
//            System.out.println("yes");
            Date date = new Date();
            if (endUserForm.getStopTime().before(date)) {
//                System.out.println("改状态");
                update = respondentInfoService.update(endUserForm, true);
                use = false;
                // 3重名 4重号码  5失败更新user 6 失败更新enduser
            } else {
                System.out.println("不改");
                update = respondentInfoService.update(endUserForm, false);
                use = true;
            }

        } else {
            System.out.println("no");
            //结束时间不能早于开始时间
            update = 2;

        }
        if (use && update == 1) {
            update = 7;
        }

        //2结束时间不能早于开始时间  3重名 4重号码  5失败更新user 6 失败更新enduser  7 状态可用 1 状态不可用 8邮箱重复
        resultVO.setCode(update);
        resultVO.setData(endUserForm);
        return resultVO;
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") String id) {
        int delete = respondentInfoService.delete(id);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(delete);
        //1 成功 2 总表删除失败 3 分表删除失败
        return resultVO;
    }

    @GetMapping("/create")
    public ResultVO create() {
//        PageVO list = endUserInfoService.search(searchForm);
        respondentInfoService.create();
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
//        resultVO.setData(list);
        return resultVO;
    }


    @PostMapping("/in")
    public ResultVO in(@RequestBody EndUserForm endUserForm) {
//        System.out.println("**********");
//        System.out.println(endUserForm.getStartTime());
//        System.out.println(endUserForm.getStopTime());
//        System.out.println(endUserForm.getUsername());

        int result = respondentInfoService.save(endUserForm);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(result);
        resultVO.setData("3用户名存在，4电话号存在，5未存入RespondentInfo ,1成功");
        return resultVO;
//        return null;
    }



    @PostMapping("/loadConferenceFile")
    public String loadConferencePersonFile(@RequestParam("file") MultipartFile file) {
        return respondentInfoService.loadConferencePersonFile(file);
//        System.out.println(file.getName());
//        System.out.println(file.getOriginalFilename());
//        System.out.println("asdasdasdasd");
//        return "成功";
    }

}
